package calbs.socket.analysis;/**
 * Created by zhangqin on 2017/8/2.
 */

import calbs.socket.pojo.req.ReqData;
import calbs.socket.pojo.req.ReqLoginMsg;
import calbs.socket.tool.CHexConver;

/**
 * 解析接口
 *
 * @author
 * @create 2017-08-02 11:28
 **/
public class LoginAnalysisImp implements AnalysisI<ReqLoginMsg>{


      @Override
      public ReqData analysisMsg(ReqData<ReqLoginMsg> reqData) {
            ReqLoginMsg reqLoginMsg = reqData.getBody();
            String content = reqLoginMsg.getContent();
            Integer device = Integer.parseInt(content.substring(0,2*4),16);
            reqLoginMsg.setDevice(device.toString());
            content = content.substring(2*4);
            Integer textLength = Integer.parseInt(content.substring(0,2*2),16);
            content = content.substring(2*2);
            String cont = CHexConver.hexStr2Str(content);
            String userName = findUserName(cont);
            reqLoginMsg.setUserName(userName);
            String passWord = findPassWord(cont);
            reqLoginMsg.setUserPassword(passWord);
            reqData.setBody(reqLoginMsg);
            return reqData;
      }
      public String findUserName(String content){
            String userName = "null";
            if (content.indexOf(';')!=-1){
                  String s = content.substring(0,content.indexOf(';'));
                  if (s.indexOf(':')!=-1){
                        userName = s.substring(s.indexOf(':')+1,s.length());
                  }
            }
            return userName;
      }
      public String findPassWord(String content){
            String passWord = "null";
            if (content.indexOf(';')!=-1){
                  String s = content.substring(content.indexOf(';')+1,content.length());
                  if (s.indexOf(':')!=-1){
                        passWord = s.substring(s.indexOf(':')+1,s.length());
                  }
            }
            return passWord;
      }

}
